// pages/login_info/index.js
import {
    Notify
} from "../../@vant/dist/notify/index"
import {
    SubmitReLogin
} from "../../api/index.js"
Page({
    options: {
        styleIsolation: "shared"
    },
    /**
     * 页面的初始数据
     */
    data: {
        checked: false,
        time: 0,
        getCode: "",
        phone: "",
        identifyingCode: "",
        show: false,
        message: "",
        isPwd: true,
        usname: '',
        pass: ''
    },

    /**
     * 发送验证码回调函数方法
     */
    handleSendCode() {
        var that = this;
        var times = 60
        var i = setInterval(function () {
            times--
            if (times < 0) {
                clearInterval(i)
            } else {
                that.setData({
                    time: times,
                    getCode: "重发" + times + "s",
                })
            }
        }, 1000)
    },
    /**
     * 密码明文密文状态切换回调函数方法
     */
    handleIsPwdEyes() {
        this.setData({
            isPwd: !this.data.isPwd
        })
    },
    onInputLowName(e) {
        this.data.usname = e.detail.value
    },
    onInputLowPass(e) {

        this.data.pass = e.detail.value
    },
    /**
     * 复选框勾选回调函数方法
     */
    handleCheckBox() {
        this.setData({
            checked: !this.data.checked
        })
    },

    /**
     * 
     * @param {*} e 
     */
    handleLoginSubmit(e) {
        const {
            phone,
            identifyingCode
        } = e.detail.value
        wx.nextTick(() => {
            if (!phone) return this.showToastTip(), this.setData({
                message: "请填写手机号"
            })
            if (!identifyingCode) return this.showToastTip(), this.setData({
                message: "请输入密码"
            })

            if (!this.data.checked) return this.showToastTip(), this.setData({
                message: "请勾选服务使用协议"
            })
        })
        if (!phone || !identifyingCode || !this.data.checked) {
            return
        }
        let data = {
            act: 'login',
            sys_type: 'app',
            phone: this.data.usname, //'17776642311'
            password: this.data.pass, // '123456'
            client_device: 'wechat_xcx'
        }
        // 提交请求执行登录
        SubmitReLogin(data).then(res => {
            console.log('res', res)
            if (res.result == 101) {
                wx.showToast({
                    title: res.msg,
                    icon: 'none',
                    duration: 2000
                  });
                      
            }  else if (res.shopid == '' || res.result == 300) {
                wx.navigateTo({
                    url: '/pages/register/index',
                })
            }  else if(res.token != undefined){
                wx.setStorageSync('TOKEN', res.token);
                wx.setStorageSync('USER', res);
                console.log('进')
                wx.reLaunch({
                    url: '/pages/index/index' 
                  });
            }
            // else if(res.shop_status == '0') {
            //     wx.navigateTo({
            //         url: '/pages/shop_auth/index',
            //     })
            // } 
            
        })
    },

    /**
     * 提示
     */
    showToastTip() {
        var that = this;
        var times = 3
        var i = setInterval(function () {
            times--
            if (times < 0) {
                that.setData({
                    show: false
                })
                clearInterval(i)
            } else {
                that.setData({
                    show: true
                })
            }
        }, 1000)
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})